import { Button, Form, Input, Select } from 'antd';
import { useEffect } from 'react';
import { addshopAPI, putshopAPI } from '../dom/httpAPI';

type propsTs = {
    title: string,
    obj: objTs | {},
    onClose: () => void,
    getpage: () => void
}
type objTs = {
    id: number,
    name: string,
    title: string,
    collect: boolean,
    line: boolean,
    people: string,
    createTime: string,
}

export default function Mydia(props: propsTs) {
    const [form] = Form.useForm()
    const onFinish = async (values: any) => {
        console.log('Success:', values);
        if (props.title == "添加") {
            let obj = { ...values, id: new Date().getTime(), createTime: new Date().toISOString().split("T")[0] }
            let res = await addshopAPI(obj)
            console.log(res);

        } else if (props.title == "修改") {
            let obj = { ...values, id: (props.obj as objTs ).id, createTime: (props.obj as objTs ).createTime }
            let res = await putshopAPI(obj)
            console.log(res);
        }
        props.onClose()
        props.getpage()
    };
    useEffect(() => {
        form.setFieldsValue(props.obj)
    }, [props.obj])

    return (
        <div>
            <Form
                form={form}

                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item
                    label="姓名"
                    name="name"
                    rules={[{ required: true, message: 'Please input your username!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="登录名"
                    name="title"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="角色"
                    name="people"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    name="collect"
                    label="是否是超级管理员"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Select>
                        <Select.Option value={true}>是</Select.Option>
                        <Select.Option value={false}>否</Select.Option>
                    </Select>
                </Form.Item>
                <Form.Item
                    name="line"
                    label="是否是在职"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Select>
                        <Select.Option value={true}>是</Select.Option>
                        <Select.Option value={false}>否</Select.Option>
                    </Select>
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        Submit
                    </Button>
                </Form.Item>
            </Form>



        </div>
    )
}
